package com.zy.nft_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.nft_system.domain.*;
import com.zy.nft_system.domain.dto.GouWuCheDto;
import com.zy.nft_system.domain.dto.OrderDto;
import com.zy.nft_system.exception.MyException;
import com.zy.nft_system.service.AccountService;
import com.zy.nft_system.service.AntiqueService;
import com.zy.nft_system.service.OrderService;
import com.zy.nft_system.service.UserantiqueService;
import com.zy.nft_system.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author 这是那头猪？
 * @version 1.0
 * @description: TODO
 * @date 2022/12/10 14:48
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    OrderService orderService;

    @Autowired
    AntiqueService antiqueService;

    @Autowired
    AccountService accountService;

    @Autowired
    UserantiqueService userantiqueService;

    @PostMapping("/insertShopChe")
    public Result insertShopChe(@RequestBody Order order){
        log.info("正在加购购物车");
        return orderService.insertShopChe(order);
    }

    @PostMapping("/goShop")
    public Result goShop(@RequestBody Order order){
        log.info("正在购买藏品");
        return orderService.goShop(order);
    }

    @GetMapping("/orderList")
    public Result orderList(Order order){
        log.info("正在查询订单");
        return orderService.orderList(order);
    }

    @GetMapping("/deleteOrderbyId")
    public Result deleteOrderbyId(Order order){
        log.info("正在更具id删除订单");
        boolean orderid = orderService.remove(new QueryWrapper<Order>().eq("orderid", order.getOrderid()));
        if(!orderid){
            log.info("删除订单失败");
            return Result.error("删除订单失败");
        }
        log.info("删除订单成功");
        return Result.success("删除订单成功", null);
    }

    @GetMapping("/listChe")
    public Result listChe(String name, int uid){
        log.info("正在查询用户购物车");
        return orderService.listChe(name, uid);
    }

    @GetMapping("/deleteOrderById")
    public Result deleteOrderById(int id){
        log.info("正在更具id删除购物车订单");
        boolean f = orderService.remove(new QueryWrapper<Order>().eq("id", id));
        if (!f) {
            log.info("删除失败");
            return Result.error("网络原因删除失败");
        }
        log.info("删除订单成功");
        return Result.success("删除订单成功", null);
    }

    @PostMapping("/deleteOrderAll")
    @Transactional(rollbackFor = MyException.class)
    public Result deleteOrderAll(@RequestBody GouWuCheDto orderDtos){
        log.info("正在更具id删除购物车订单");
        OrderDto[] dtos = orderDtos.getOrderDtos();
        if (dtos.length == 0){
            log.info("请选择要删除的订单");
            return Result.error("请选择要删除的订单");
        }
        for (OrderDto o:dtos) {
            if (!orderService.remove(new QueryWrapper<Order>().eq("id",o.getId()))) {
                log.info("删除失败");
                return Result.error("网络原因删除失败");
            }
        }
        log.info("删除订单成功");
        return Result.success("删除订单成功", null);
    }

    @PostMapping("/delectOrderall")
    @Transactional(rollbackFor = MyException.class)
    public Result delectOrderall(@RequestBody GouWuCheDto orderDtos){
        log.info("正在更具订单id删除购物车订单");
        OrderDto[] dtos = orderDtos.getOrderDtos();
        if (dtos.length == 0){
            log.info("请选择要删除的订单");
            return Result.error("请选择要删除的订单");
        }
        for (OrderDto o:dtos) {
            if (!orderService.remove(new QueryWrapper<Order>().eq("orderid",o.getOrderid()))) {
                log.info("删除失败");
                return Result.error("网络原因删除失败");
            }
        }
        log.info("删除订单成功");
        return Result.success("删除订单成功", null);
    }
    @PostMapping("/JieSuan")
    @Transactional(rollbackFor = MyException.class)
    public Result JieSuan(@RequestBody GouWuCheDto orderDtos){
        log.info("正在结算购物车");
        OrderDto[] dtos = orderDtos.getOrderDtos();
        for (OrderDto o:dtos) {
            Order order = orderService.getOne(new QueryWrapper<Order>().eq("id", o.getId()));
            log.info("正在查看账户金额是否够付款");
            Account one = accountService.getOne(new QueryWrapper<Account>().ge("overage", order.getSell()).eq("uid", order.getUid()));
            if (one == null) {
                log.info("账户金额不足无法下单");
                throw new MyException("账户金额不足无法下单");
            }
            log.info("账户金额充足");

            log.info("正在付款");
            one.setOverage(one.getOverage()-order.getSell());
            boolean uid = accountService.update(one, new QueryWrapper<Account>().eq("uid", order.getUid()));
            if (!uid) {
                log.info("未知原因付款失败");
                throw new MyException("网络原因付款失败");
            }
            log.info("付款成功");

            log.info("正在查看仓库数量是否充足");
            Antique count = antiqueService.getOne(new QueryWrapper<Antique>().ge("count", o.getNumber()).eq("id", order.getAntiqueinfo()));
            if (count == null) {
                log.info("仓库藏品不足无法下单");
                throw new MyException("仓库藏品不足无法下单");
            }
            log.info("仓库藏品充足");
            log.info("正在出货");
            count.setCount(count.getCount()-order.getNumber());
            if (!antiqueService.update(count, new QueryWrapper<Antique>().eq("id", order.getAntiqueinfo()))) {
                log.info("未知原因出货失败");
                throw new MyException("网络原因下单失败");
            }
            log.info("出货成功");

            log.info("正在生成订单");
            order.setOrderid(new Random().nextInt(999999));
            order.setStatus(1);
            order.setSendtime(DateUtils.dataFormat(new Date()));
            boolean update = orderService.update(order, new QueryWrapper<Order>().eq("id", order.getId()));
            if (!update) {
                log.info("生成订单失败");
                throw new MyException("网络原因生成订单失败");
            }
            log.info("生成订单成功");

            for (int i = 0; i < order.getNumber(); i++) {
                log.info("正在发货");
                List<Userantique> list = userantiqueService.list(new QueryWrapper<Userantique>().eq("uid", order.getUid()).eq("aid", order.getAntiqueinfo()));
                int cout;
                if (list.isEmpty()){
                    cout=1;
                    Userantique userantique = new Userantique();
                    userantique.setName(count.getName());
                    userantique.setNumber(cout);
                    userantique.setPurchaseprice(count.getSell());
                    userantique.setStatus(1);
                    userantique.setImgsrc(count.getImgsrc());
                    userantique.setCreatetime(DateUtils.dataFormat(new Date()));
                    userantique.setAid(count.getId());
                    userantique.setUid(order.getUid());
                    if (! userantiqueService.save(userantique)) {
                        log.info("发货失败");
                        throw new MyException("发货失败");
                    }
                    log.info("发货成功");
                }else {
                    cout=list.size();
                    Userantique userantique = (Userantique) list.get(0);
                    userantique.setNumber(cout+1);
                    userantique.setId(null);
                    if (!userantiqueService.save(userantique)){
                        log.info("发货失败");
                        throw new MyException("发货失败");
                    }
                    log.info("发货成功");
                }
            }
        }
        return Result.success("结算成功", null);
    }

    @GetMapping("/queryAll")
    public Result queryAll(){
        log.info("正在查询收益基本信息");
        return orderService.queryAll();
    }

    @GetMapping("/sellsAll")
    public Result sellsAll(){
        log.info("正在查询前7天收益基本信息");
        return orderService.sellsAll();
    }
}
